package vn.demand.dragon.view
{
	import com.adobe.utils.StringUtil;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import flexed.widgets.alerts.alert;
	
	import mx.core.Application;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import vn.demand.dragon.CommonCommand;
	import vn.demand.dragon.model.CompanyProxy;
	import vn.demand.dragon.model.MasterdataProxy;
	import vn.demand.dragon.model.vo.Company;
	import vn.demand.dragon.view.components.QuickCustomer;;
	
	public class QuickCompanyMediator extends Mediator
	{
		
		public static var NAME:String = "QuickCompanyMediator";
		
		private var masterdataProxy:MasterdataProxy;
		private var companyProxy:CompanyProxy;
		private var company:Company;
		
		public function QuickCompanyMediator(mediatorId:String)
		{
			super(mediatorId);
			masterdataProxy = facade.retrieveProxy(MasterdataProxy.NAME) as MasterdataProxy;
			companyProxy = facade.retrieveProxy(CompanyProxy.NAME) as CompanyProxy;
		}

		public function get companyForm():QuickCustomer{
			return viewComponent as QuickCustomer;
		}
		
		public function show(company:Company):void {
			this.company = company;
			var popup:QuickCustomer = PopUpManager.createPopUp(Application.application as DisplayObject, QuickCustomer, true) as QuickCustomer;
			setViewComponent(popup);
			companyForm.com_status.dataProvider = masterdataProxy.companyStatusesDP;
			companyForm.type.dataProvider = masterdataProxy.companyTypesDP;
			companyForm.classification.dataProvider = masterdataProxy.classificationDP;
			companyForm.division.dataProvider = masterdataProxy.divisionsDP;
			companyForm.cost_center.dataProvider = masterdataProxy.costCentersDP;
			companyForm.resource.dataProvider = masterdataProxy.usersDP;
			
			companyForm.code.text = company.company_code;
			companyForm.com_name.text = company.company_name;
			companyForm.com_status.selectedData = company.company_status;
			companyForm.type.selectedData = company.company_type;
			companyForm.exact_code.text = company.company_exact_code;
			companyForm.vat_code.text = company.company_vat_code;
			companyForm.ledger_1.text = company.company_ledge1;
			companyForm.ledger_2.text = company.company_ledge2;
			companyForm.address.text = company.company_address1;
			companyForm.tel.text = company.company_phone1;
			companyForm.fax.text = company.company_fax;
			companyForm.classification.selectedIndex = company.company_classification;
			companyForm.division.selectedData = company.company_division;
			companyForm.cost_center.selectedData = company.company_costcenter;
			companyForm.resource.selectedData = company.company_resource;
			companyForm.description.text = company.company_description;
			
			if (company.company_id > 0) {
				companyForm.title = "Edit customer: " + company.company_code;
			} else {
				companyForm.title = "Add customer";
			}
			
			companyForm.addEventListener("addCompany", onAddCompany);
			companyForm.addEventListener("cancelAddCompany", onClose);
			PopUpManager.centerPopUp(popup);
		}
		
		public function onAddCompany(event:Event):void {
			if (!doValidate()) {
				alert.error(errorStrings);
				return;
			}
			company.company_code = companyForm.code.text;
			company.company_name = companyForm.com_name.text;
			company.company_status = companyForm.com_status.selectedData;
			company.company_type = companyForm.type.selectedData;
			company.company_exact_code = companyForm.exact_code.text;
			company.company_vat_code = companyForm.vat_code.text;
			company.company_ledge1 = companyForm.ledger_1.text;
			company.company_ledge2 = companyForm.ledger_2.text;
			company.company_address1 = companyForm.address.text;
			company.company_phone1 = companyForm.tel.text;
			company.company_fax = companyForm.fax.text;
			company.company_classification = companyForm.classification.selectedData;
			company.company_division = companyForm.division.selectedData;
			company.company_costcenter = companyForm.cost_center.selectedData;
			company.company_resource = companyForm.resource.selectedData;
			company.company_description = companyForm.description.text;
			
			companyProxy.saveCompany(company);
		}
		
		public function doValidate():Boolean {
			errorStrings = "";
			if (StringUtil.trim(companyForm.code.text).length == 0) {
				errorStrings += "Please input [Customer Code]\n";
			}
			if (StringUtil.trim(companyForm.com_name.text).length == 0) {
				errorStrings += "Please input [Customer Name]\n";
			}
			if (companyForm.com_status.selectedIndex == 0) {
				errorStrings += "Please select [Status]\n";
			}
			if (companyForm.type.selectedIndex == 0) {
				errorStrings += "Please select [Type]\n";
			}
			return errorStrings.length == 0	? true : false;
		}
		
		public function onClose(event:Event):void {
			if (null != companyForm) {
				PopUpManager.removePopUp(companyForm);
			}
		}
		
		override public function listNotificationInterests():Array 
        {
            return [ 
            	CommonCommand.COMPANY_ADDED,
            	CommonCommand.CUSTOMERS_UPDATED_SUCESSFUL,
            	CommonCommand.COMPANY_ADD_FAILED,
					 ];
        }
        
        override public function handleNotification( note:INotification ):void 
        {
            switch ( note.getName() ) 
			{
				case CommonCommand.COMPANY_ADDED:
					onClose(new Event("closeQuickCompany"));
					break;
				case CommonCommand.CUSTOMERS_UPDATED_SUCESSFUL:
					onClose(new Event("closeQuickCompany"));
					break;	
				case CommonCommand.COMPANY_ADD_FAILED:
					errorStrings = note.getBody() as String;
					alert.error(errorStrings);
                default:
                	break;
            }
        }
        
        private var errorStrings:String;
	}
}